package com.example.match.mui;

import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.match.R;
import com.example.match.bz.Stuitem;
import com.example.mylibrary.config.Config;
import com.example.mylibrary.data.DBManager;
import com.example.mylibrary.data.DataDao;
import com.example.mylibrary.data.Item;
import com.example.mylibrary.data.ItemGrop;
import com.example.mylibrary.data.Schedule;
import com.example.mylibrary.data.StudentInfo;
import com.example.mylibrary.data.StudentItem;
import com.example.mylibrary.util.ActivityCollector;
import com.example.mylibrary.util.ImageUtil;
import com.example.mylibrary.util.SysApplication;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.Bind;
import butterknife.ButterKnife;



public class InfoDisplayActivity extends AppCompatActivity  {
    public static final String FILE_IMG ="/sdcard/FP-KTA2203/img/";
    @Bind(R.id.img_id_photo)
    ImageView idPhoto;
    @Bind(R.id.img_Scene_photo)
    ImageView scenePhoto;
    @Bind(R.id.ed_name1)
    EditText edName;
    @Bind(R.id.ed_stuCode)
    TextView edStuCode;
    @Bind(R.id.ed_sex1)
    EditText edSex;
    @Bind(R.id.ed_company1)
    EditText edCompany;
    @Bind(R.id.ed_idcard1)
    EditText edIdCard;
    @Bind(R.id.rv_InfoItemview1)
    RecyclerView recyclerView;
    private StudentInfo studentInfo;
    private Context mContext;
    private MyAdapter myAdapter;
    private List<Item> item;
    private List<Stuitem> stuitemList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info_display);
        mContext = this;
        SysApplication.getInstance().addActivity(this);
        ButterKnife.bind(this);
        ActionBar actionBar =  getActionBar();
        if(actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        initDatas();
        initViews();

    }

    private void initDatas() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();

         String  imgCode = getIntent().getStringExtra("stuCode");
        Log.e( "idcardNum1:----> " ,imgCode+"");

       List<StudentInfo> studentInfos = DataDao.queryInfo(imgCode);
        for (StudentInfo infos : studentInfos) {
            studentInfo = infos;
            Log.e( "initDatasstudentInfo: ",studentInfo.getStudentID().toString() +"");
        }
      edName.setText(studentInfo.getStudentName());
       edStuCode.setText( studentInfo.getStudenCode());
        edSex.setText(studentInfo.getSex()== 0?"男":"女");
        edCompany.setText(studentInfo.getAddress());
        edIdCard.setText(studentInfo.getIdCard());


        //获取本地学生照片

        File file = new File(FILE_IMG+studentInfo.getIdCard()+".jpg");
        Log.e( "img-->: ",file+"" );

        if (file.exists()) {
            Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath());
            //将图片显示到ImageView中
            idPhoto.setImageBitmap(bm);

        }


        byte[] snapshotFeature = getIntent().getByteArrayExtra("snapshotFeature");
        if (snapshotFeature != null) {
            final Bitmap b = ImageUtil.getRotateBitmap(BitmapFactory
                            .decodeByteArray(snapshotFeature, 0, snapshotFeature.length),
                    Config.displayOrientation);
            scenePhoto.setImageBitmap(b);
        }
    }
    private void initViews() {

        stuitemList = new ArrayList<>();
        Item items = new Item();

        List<Long> sc = new ArrayList<>();
        //根据学生id查询学生项目表信息
        List<StudentItem> studentItem = DBManager.getInstance(mContext).queryStudentItem( studentInfo.getStudentID());
        Log.e( "onClick: item name ", studentItem.get(0).getItemID()+"");
        for (StudentItem s:studentItem){
            //根据项目id 查询项目名称
            item = DBManager.getInstance(mContext).queryItemName( s.getItemID());
            for (int k = 0; k < item.size(); k++) {
                Stuitem stuitem = new Stuitem();
                items = item.get(k);
                stuitem.setTextTrackNo(k+1);
                stuitem.setItemName(  items.getItemName());
                Log.e( "onClick: itemname",items.getItemName() );
                //根据项目id   查询项目分组信息
               List<ItemGrop> itemGrops = DBManager.getInstance(mContext).itemGrop(items.getItemID());
                for (ItemGrop itemGrop:itemGrops) {
                    Set ss = new HashSet<>();
                    ss.add(itemGrop.getScheduleID());
                    Log.e( "onClick: Set",ss+"" );
                    sc.addAll(ss);
                }

                for (int z = 0; z < sc.size(); z++) {
                    Schedule schedule = DBManager.getInstance(mContext).scheduleTime(sc.get(z));
                    stuitem.setScheduleTimes(schedule.getScheduletime());
                }

                stuitemList.add(stuitem);

            }

            for (Stuitem  s1: stuitemList){
                Log.e("initViews:Stuitem", s1.getItemName() );
            }
        }

        if (myAdapter == null) {
            myAdapter = new MyAdapter();
        }

        myAdapter.setMyAdapter(stuitemList);
        recyclerView.setAdapter(myAdapter);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:   //返回键的id
                finish();
                break;
        }
        return true;

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        startActivity(new Intent(this,BlankPageActivity.class));
        ActivityCollector.removeActivity(this);

    }


    class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
            private List<Stuitem> stuitemList ;
        public void setMyAdapter(List<Stuitem> stuitemList) {
            this.stuitemList = stuitemList;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            InfoViewHolder1 infoViewHolder1 = new InfoViewHolder1(LayoutInflater.from(InfoDisplayActivity.this).inflate(R.layout.item_list_view_infos, parent, false));
            return infoViewHolder1;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            InfoViewHolder1 infoViewHolder1 = (InfoViewHolder1) holder;
            infoViewHolder1.trackNo.setText(String.valueOf(position + 1));
            infoViewHolder1.textStuItem.setText(stuitemList.get(position).getItemName());
            infoViewHolder1.textSchedulTime.setText(stuitemList.get(position).getScheduleTimes());
        }

        @Override
        public int getItemCount() {
            return stuitemList.size();
        }

        class InfoViewHolder1 extends RecyclerView.ViewHolder {
            @Bind(R.id.tv_No)
            TextView trackNo;
            @Bind(R.id.tv_itemName)
            TextView textStuItem;
            @Bind(R.id.tv_schedule_time)
            TextView textSchedulTime;


            public InfoViewHolder1(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }
}
